package com.saltlux.tdd.paginate;

public class GroupedPaginate implements Paginate {
	private int _totalDocumentCount;
	private int _pageSize;
	private int _currentPage;
	private int _pagegroupSize;
	private int _pagegroup;

	public GroupedPaginate(int totalDocumentCount, int currentPage,
			int pageSize, int pagegroupSize) {
		_totalDocumentCount = totalDocumentCount;
		_pageSize = pageSize;
		_currentPage = currentPage > 0 ? currentPage : 1;
		_pagegroupSize = pagegroupSize;
		_pagegroup = (int) Math.ceil(_currentPage / (double) _pagegroupSize);
	}

	public int getFirstPage() {
		return 1;
	}

	public int getLastPage() {
		return (int) Math.ceil(_totalDocumentCount / ((double) _pageSize));
	}

	public int getCurrentPage() {
		return _currentPage;
	}

	public int getListStartPage() {
		return (_pagegroup - 1) * _pagegroupSize + 1;
	}

	public int getListEndPage() {
		return _pagegroup * _pagegroupSize;
	}

	public int getPreviousPage() {
		int result = (_pagegroup - 1) * _pagegroupSize;
		if (result == 0)
			result = 1;
		return result;
	}

	public int getNextPage() {
		int result = getListEndPage() + 1;
		return Math.min(result, getLastPage());
	}
	public int getTotalDocumentCount(){
		return _totalDocumentCount;
	} 
	public int getPageSize(){
		return _pageSize;
	}
	public int getPagegroupSize(){
		return _pagegroupSize;
	}
}
